﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BPM.Service;
using BPM.Entity;

namespace WebTest
{
	[TestClass]
	public class UserTest : TestBase
	{
		[ClassInitialize()]
		public static void ClassInitialize(TestContext testContext)
		{
			InitializeApplication();
		}

		private User _testUser;

		[TestInitialize()]
		public override void TestInitialize()
		{
			var svc = new UserService();
			var userName = "TEST_USER";
			var res = svc.AddUser(userName, userName, "1", out this._testUser);
			Assert.IsTrue(this._testUser != null);
		}

		[TestCleanup()]
		public override void TestCleanup()
		{
			var svc = new UserService();
			Assert.IsTrue(svc.Delete(this._testUser.Id));

			var barcode = new BarcodeService().GetById(this._testUser.BarcodeId);
			Assert.IsNull(barcode);

			this._testUser = null;
		}

		//[TestMethod]
		//public void Entities_Add100Users()
		//{
		//    var svc = new UserService();

		//    User user = null;

		//    for (int i = 0; i < 100; i++)
		//    {
		//        svc.AddUser(
		//            "random_" + i,
		//            "random_" + i,
		//            "1",
		//            out user
		//            );
		//    }
		//}

		[TestMethod]
		public void UserTest_AllowNotValidUser()
		{
			Assert.IsTrue(new UserService().AllowNotValidUser(this._testUser.Id));

			var user = new UserService().GetByUserName(this._testUser.UserName);
			Assert.IsNotNull(user);
			Assert.IsTrue(user.IsValid);
		}

		[TestMethod]
		public void UserTest_Logon_LazyLoad()
		{
			User user = null;
			Assert.IsTrue(new UserService().Logon("admin", "1", out user));
			Assert.IsNotNull(user.RoleList);
			Assert.IsTrue(user.RoleList.Count > 0);
			Assert.IsNotNull(user.Barcode);
			Assert.IsNotNull(user.Barcode.BarcodeCategory);
		}
	}
}